package com.white.algebra.service.impl;

import com.white.algebra.dao.PermissionDao;
import com.white.algebra.dao.RoleDao;
import com.white.algebra.dao.impl.PermissionDaoImpl;
import com.white.algebra.dao.impl.RoleDaoImpl;
import com.white.algebra.domain.Permission;
import com.white.algebra.service.PermissionService;


import java.sql.SQLException;
import java.util.List;


public class PermissionServiceImpl implements PermissionService {

    private PermissionDao permissionDao=new PermissionDaoImpl();
    @Override
    public List<Permission> findAll() throws SQLException {
        return permissionDao.findAll();
    }

    @Override
    public void save(Permission permission) throws SQLException {
        permissionDao.save(permission);
    }

    @Override
    public void deleteRole_PermissionByPermissionId(String id) throws SQLException {
        permissionDao.deleteRole_PermissionByPermissionId(id);
        permissionDao.deletePermissionById(id);
    }

    @Override
    public List<Permission> findById(String id) throws SQLException {
        return permissionDao.findById(id);
    }

    @Override
    public void addRoleByPermissionId(String permissionId, String roleId) throws SQLException {
        permissionDao.addpermission(permissionId,roleId);
    }

    @Override
    public void deleteRoleById(String permissionId, String roleId) throws SQLException {
        permissionDao.deleteRole_PermissionByPermissionAndRoleId(permissionId,roleId);
    }

    @Override
    public Permission findPermissionById(String id) throws SQLException {
        Permission permission=permissionDao.findPermissionById(id);
        RoleDao roleDao=new RoleDaoImpl();
        permission.setRoles(roleDao.findRolesByPermissionId(id));
        return permission;
    }
}
